package org.sasha.roulette.actionbar;


import javax.inject.Inject;

import com.github.peholmst.mvp4vaadin.AbstractView;
import com.vaadin.data.Container;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ChameleonTheme;

@SuppressWarnings("serial")
public class ActionBarViewImpl extends AbstractView<ActionBarView, ActionBarPresenter> implements ActionBarView {
    
    private final FastSpinCreationView fastSpinCreationView;
    
    private Button createNewSessionButton = new Button("Создать сессию", new Button.ClickListener() {
        @Override
        public void buttonClick(ClickEvent event) {
            getPresenter().initializeNewSession();
        }
    });
    
    private ComboBox sessionSelector = new ComboBox();
    
    private Panel actionBar = new Panel(new HorizontalLayout(){{
        setSizeFull();
        setMargin(true);
        setSpacing(true);
        addComponent(createNewSessionButton);
        final Label textLabel = new Label("&nbsp &nbsp Выбрать сессию:", Label.CONTENT_XHTML);
        textLabel.setSizeUndefined();
        addComponent(textLabel);
        addComponent(sessionSelector);
        setExpandRatio(sessionSelector, 1f);
        setComponentAlignment(sessionSelector, Alignment.TOP_LEFT);
        setComponentAlignment(createNewSessionButton, Alignment.TOP_CENTER);
    }});
    
    private VerticalLayout root = new VerticalLayout();
    
    @Inject
    public ActionBarViewImpl(final ActionBarPresenter presenter, final FastSpinCreationView fastSpinCreationView) {
        super(false);
        this.fastSpinCreationView = fastSpinCreationView;
        construct();
        setPresenter(presenter);
        presenter.setView(this);
        init();
    }
    
    private void construct() {
        createNewSessionButton.addStyleName(ChameleonTheme.BUTTON_SMALL);
        createNewSessionButton.setSizeUndefined();
     
        actionBar.addStyleName("v-action-bar");
        actionBar.setSizeUndefined();
        actionBar.setWidth("100%");

        sessionSelector.setNullSelectionAllowed(false);
        sessionSelector.setNullSelectionItemId(null);
        sessionSelector.setImmediate(true);
        
        root.setWidth("100%");
        root.addComponent(actionBar);
        root.addComponent(fastSpinCreationView.getViewComponent());
    }

    private final ValueChangeListener valueHandler = new ValueChangeListener() {

        @Override
        public void valueChange(ValueChangeEvent event) {
            getPresenter().changeCurrentSession(event.getProperty().getValue());
            
        }
    };
    
    @Override
    public void setSessionList(final Container data) {
        sessionSelector.setContainerDataSource(data);
        sessionSelector.removeListener(valueHandler);
        sessionSelector.setItemCaptionPropertyId("CREATION_TIME");
        sessionSelector.setValue(getPresenter().getDefaultSessionId());
        sessionSelector.addListener(valueHandler);
    }
    
    @Override
    public ComponentContainer getViewComponent() {
        return root;
    }

}
